import { ref , onMounted , onUnmounted } from "vue";
import type {Query} from "@/interface/Query";
import type { Resolution } from "@/interface/Resolution"
import type {TableColumnsType} from "ant-design-vue";
import { listApi , operatorApi , removeApi } from "@/controller/ResolutionApi"
import { message } from "ant-design-vue";
import dayjs from "dayjs";


export default function () {

    let queryModel = ref({
        page: 1,
        size: 10,
        keyWord: ''
    } as Query);

    let searchBox = ref<boolean>(true);


    onMounted(() => {
        list();
    })


    let resolutionVoteRef = ref(null);



    let data = ref<Resolution[]>([]);
    let total = ref<number>(0);
    function list() {
        listApi(queryModel.value).then( res => {
            if ( res.code == 200) {
                data.value = res.data.records;
                total.value = parseInt(res.data.totalRow);
            }
        })
    }

    let voteStore = ref({
        open: false,
        id: '',
        title: ''
    })
    function lookVoteInfo(id : string,title : string) {
        voteStore.value.open = true;
        voteStore.value.id = id;
        voteStore.value.title = title;
    }

    function resetQuery() {
        queryModel.value = {
            page: 1,
            size: 10,
            keyWord: ''
        } as Query;
        list();
    }

    function remove(ids ? : string[]) {
        console.log(ids);
        if (ids) {
            removeApi(ids).then( res => {
                if ( res.code == 200) {
                    message.success('删除成功');
                    list();
                }else {
                    message.error("删除失败");
                }
            });
        }else {
            if(rowsData.value.length == 0) {return;}
            remove(rowsData.value.map( item => item.id));
        }
    }


    let columns = ref<TableColumnsType>([
        {
            title: '标题',
            dataIndex: 'title',
            width: 100,
            resizable: true,
            fixed: 'left',
            align: 'center'
        },
        {
            title: '封面',
            dataIndex: 'cover',
            width: 120,
            resizable: true,
            align: 'center'
        },
        {
            title: '类型',
            dataIndex: 'type',
            width: 120,
            resizable: true,
            align: 'center'
        },
        {
            title: '创建人',
            dataIndex: 'pushUser',
            width: 180,
            resizable: true,
            align: 'center'
        },
        {
            title: '投票类型',
            dataIndex: 'voteType',
            width: 120,
            resizable: true,
            align: 'center'
        },
        {
            title: '是否匿名',
            dataIndex: 'isAnonymity',
            width: 160,
            resizable: true,
            align: 'center'
        },
        {
            title: '是否多选',
            dataIndex: 'multiple',
            width: 160,
            resizable: true,
            align: 'center'
        },
        {
            title: '最多几次',
            dataIndex: 'atMost',
            width: 140,
            resizable: true,
            align: 'center'
        },
        {
            title: '参与方式',
            dataIndex: 'dec',
            width: 140,
            resizable: true,
            align: 'center'
        },
        {
            title: '状态',
            dataIndex: 'status',
            width: 80,
            resizable: true,
            align: 'center'
        },
        {
            title: '查看方式',
            dataIndex: 'showWay',
            width: 130,
            resizable: true,
            align: 'center'
        },
        {
            title: '开始时间',
            dataIndex: 'startTime',
            width: 130,
            resizable: true,
            align: 'center'
        },
        {
            title: '结束时间',
            dataIndex: 'endTime',
            width: 130,
            resizable: true,
            align: 'center'
        },
        {
            title: '操作',
            dataIndex: 'action',
            fixed: 'right',
            width: 130,
            resizable: true,
            align: 'center'
        }
    ]);

    function handleResizeColumn(w : any, col : any) {
        col.width = w;
    }

    let rowsData  = ref<Resolution[]>([])
    function selectData(selectedRowKeys : any, selectedRows : Resolution[]) {
        rowsData.value = selectedRows;
    }

    function pageChange(page : number, size : number) {
        queryModel.value.page = page;
        queryModel.value.size = size;
        list();
    }

    let operatorStore = ref({
        open : false,
        resolution: {} as Resolution,
        cmd : 0
    });


    function operator(cmd : number,data ? : Resolution) {
        operatorStore.value.open = true;
        operatorStore.value.cmd = cmd;
        if (data) {
            operatorStore.value.resolution = data;
            operatorStore.value.resolution.startTime = dayjs(operatorStore.value.resolution.startTime,'YYYY-MM-DD') as any;
            operatorStore.value.resolution.endTime = dayjs(operatorStore.value.resolution.endTime,'YYYY-MM-DD') as any;
        }
    }

    function cancel(status : boolean) {
        operatorStore.value.open = false;
        operatorStore.value.resolution = {} as Resolution;
    }

    function submit(data : Resolution) {
        operatorApi(data).then(res => {
           if ( res.code == 200) {
               list();
               cancel(true);
               operatorStore.value.open = false;
               message.success( operatorStore.value.cmd == 1 ? '编辑成功' : '新增成功');
           }else {
               message.error( operatorStore.value.cmd == 1 ? '编辑失败' : '新增失败');
           }
        });
    }

    function closeVoteInfo() {
        voteStore.value.open = false;
    }


    return {
        queryModel , pageChange ,  columns , list , searchBox ,  operator,
        operatorStore , handleResizeColumn , resetQuery , data , total ,
        selectData , rowsData , cancel , submit , remove , voteStore ,
        lookVoteInfo , resolutionVoteRef , closeVoteInfo
    }
}
